Conditions | 17 |
Paths | 23 |
Total Lines | 47 |
Code Lines | 34 |
Lines | 1 |
Ratio | 2.13 % |
Changes | 0 |
Complex classes like strip-json-comments.js ➔ stripJsonComments often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /*! |
||
11 | function stripJsonComments(str) { |
||
12 | var currentChar; |
||
13 | var nextChar; |
||
14 | var insideString = false; |
||
15 | var insideComment = false; |
||
16 | var ret = ''; |
||
17 | |||
18 | for (var i = 0; i < str.length; i++) { |
||
19 | currentChar = str[i]; |
||
20 | nextChar = str[i + 1]; |
||
21 | |||
22 | if (!insideComment && str[i - 1] !== '\\' && currentChar === '"') { |
||
23 | insideString = !insideString; |
||
24 | } |
||
25 | |||
26 | if (insideString) { |
||
27 | ret += currentChar; |
||
28 | continue; |
||
29 | } |
||
30 | |||
31 | if (!insideComment && currentChar + nextChar === '//') { |
||
32 | insideComment = 'single'; |
||
33 | i++; |
||
|
|||
34 | } else if (insideComment === 'single' && currentChar + nextChar === '\r\n') { |
||
35 | insideComment = false; |
||
36 | i++; |
||
37 | View Code Duplication | } else if (insideComment === 'single' && currentChar === '\n') { |
|
38 | insideComment = false; |
||
39 | } else if (!insideComment && currentChar + nextChar === '/*') { |
||
40 | insideComment = 'multi'; |
||
41 | i++; |
||
42 | continue; |
||
43 | } else if (insideComment === 'multi' && currentChar + nextChar === '*/') { |
||
44 | insideComment = false; |
||
45 | i++; |
||
46 | continue; |
||
47 | } |
||
48 | |||
49 | if (insideComment) { |
||
50 | continue; |
||
51 | } |
||
52 | |||
53 | ret += currentChar; |
||
54 | } |
||
55 | |||
56 | return ret; |
||
57 | } |
||
58 | |||
65 |